from django.test import TestCase
from util.const import GZ_REST_BASE_URL
from util.jiafu_rest_api import RestApi
from condition_order.models import ConditionOrder
from condition_order.services.condition_order_logic import ConditionOrderThread, recover_condition_order
import simplejson
import requests
# Create your tests here.

# unit test cmd
# python manage.py test condition_order.tests.ConditionOrderThreadTestCase.test_create_order --keepdb
# 如果数据模型更改后，记得去掉keepdb选项，重建测试数据库。


def get_cookies(name='600001', password='123456'):
    restApi = RestApi(GZ_REST_BASE_URL)
    res = restApi.login(name=name, password=password)
    cookie = restApi.get_cookies()
    return cookie


class ConditionOrderThreadTestCase(TestCase):
    def setUp(self):
        self.order = ConditionOrder(order_type="LIMIT", side="BUY", condition_price=0.0015560, order_status='RUNNING',
                               price_type=0, symbol='BNBBTC', qty=1, compare_type=0, price=0.00156)

        self.cookie = get_cookies()
        self.order.save()

    def test_create_order(self):
        cot = ConditionOrderThread(self.cookie, self.order)
        res = cot.create_order()
        self.assertEqual(res["errno"], 0, 'create condition order fail %s' % res.get("message"))

    def test_thread_run(self):
        cot = ConditionOrderThread(self.cookie, self.order)
        cot.start()
        cot.join()


class ConditionOrderRest(TestCase):

    def setUp(self):
        self.cookies = get_cookies()
        order = ConditionOrder(order_type="LIMIT", side="BUY", condition_price=0.0015560, order_status='RUNNING',
                               price_type=0, symbol='BNBBTC', qty=1, compare_type=0, price=0.00156,
                               cookies=simplejson.dumps(self.cookies))

        order.save()

    def test_post(self):
        url = "http://127.0.0.1:8001/gz_server/condition_order/"
        headers = {'content-type': 'application/json'}
        params = {"symbol": "BNBBTC", "order_type": "LIMIT", "price": 0.00156,
                  "qty": 1, "side": "BUY",
                  "price_type": 0,
                  "compare_type": 0,
                  "condition_price": 0.0016,
                  "cookies": get_cookies(),
                  "user_name": "600001"
                 }
        res = requests.post(url, data=simplejson.dumps(params), cookies=self.cookies, timeout=2, headers=headers)
        print(res.text)
        self.assertEqual(res.status_code, 200, 'HTTP Sever Internal Error')


    def test_get(self):
        url = "http://127.0.0.1:8001/gz_server/condition_order/"
        res = requests.get(url,  params={"user_name": "600001"},  timeout=2)
        print(simplejson.loads(res.text))

    def test_put(self):
        url = "http://127.0.0.1:8001/gz_server/condition_order/"
        res = requests.put(url,  data=simplejson.dumps({"order_status": "STOPPED", "condition_order_id": 3}),
                           cookies=self.cookies, timeout=2)
        print(simplejson.loads(res.text))
